import router from "@/router";
import store from "@/store"
import { getInfoAPI } from '@/api/dashboard'

let whiteList = ['/login']

router.beforeEach(async (to, from, next) => {
  // 已登录
  if (store.state.user.token) {

    // 获取用户信息
    //    无用户信息时再发出请求
    if (!store.state.user.userInfo.id) {
      const res = await getInfoAPI()
      store.dispatch('user/SET_USERINFO', res.data)
      // 获取动态路由后再次调用本前置首位
      next(to)
    } else {
    }
    // 不可以返回登录界面
    if (to.path.toLocaleLowerCase() == '/login') {
      next(from)
    } else {
      next()
    }
  }
  // 未登录
  else {
    // next('/login')
    if (whiteList.includes(to.path.toLocaleLowerCase())) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router